package com.appwoo.txtws.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
import com.appwoo.txtws.activity.CropActivity;
import com.appwoo.txtws.bean.WallpaperBean;
import com.appwoo.txtws.component.DBHelper;
import com.appwoo.txtws.component.Global;
import com.appwoo.txtws.component.ImageHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class WallpaperListAdapter extends BaseAdapter
{
  public static final String WALLPAPER_APP_INNER = "appInner";
  public static final String WALLPAPER_DOWNLOADED = "downloaded";
  public static final String WALLPAPER_PHONE_ALBUM = "phoneAlbum";
  public static final String WALLPAPER_WEB = "web";
  private int _numColumns;
  private Context context;
  private int height;
  private LayoutInflater inflater;
  private ArrayList<WallpaperBean> listWallpaperBean;
  private String pathMemory = "data/tylw/";
  private String pathSDCard = "sdcard/tylw/";
  public boolean tipNotNet = false;
  public boolean tipStorageSize = false;
  public boolean tipWallpaperDownloadFail = false;
  private int width;

  public WallpaperListAdapter()
  {
  }

  public WallpaperListAdapter(Context paramContext, ArrayList<WallpaperBean> paramArrayList, String paramString, int paramInt1, int paramInt2, int paramInt3)
  {
    this.context = paramContext;
    this.listWallpaperBean = paramArrayList;
    this.inflater = ((LayoutInflater)this.context.getSystemService("layout_inflater"));
    this._numColumns = paramInt1;
    this.width = ((paramInt2 - 6) / this._numColumns);
    this.height = ((paramInt3 - 10) / 4);
  }

  public void addData(List<WallpaperBean> paramList)
  {
    Iterator localIterator = paramList.iterator();
    while (true)
    {
      if (!localIterator.hasNext())
        return;
      WallpaperBean localWallpaperBean = (WallpaperBean)localIterator.next();
      this.listWallpaperBean.add(localWallpaperBean);
    }
  }

  public void gc()
  {
    System.gc();
  }

  public int getCount()
  {
    if (this.listWallpaperBean.size() % this._numColumns == 0)
      return this.listWallpaperBean.size() / this._numColumns;
    return 1 + this.listWallpaperBean.size() / this._numColumns;
  }

  public Object getItem(int paramInt)
  {
    return null;
  }

  public long getItemId(int paramInt)
  {
    return 0L;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    WallpaperViewHolder localWallpaperViewHolder;
    if (paramView == null)
    {
      paramView = this.inflater.inflate(2130903228, null);
      localWallpaperViewHolder = new WallpaperViewHolder();
      localWallpaperViewHolder.image01 = ((ImageView)paramView.findViewById(2131493635));
      localWallpaperViewHolder.image02 = ((ImageView)paramView.findViewById(2131493636));
      paramView.setTag(localWallpaperViewHolder);
    }
    while (true)
    {
      WallpaperBean localWallpaperBean1 = (WallpaperBean)this.listWallpaperBean.get(paramInt * 2);
      localWallpaperViewHolder.image01.setLayoutParams(new LinearLayout.LayoutParams(this.width, this.height));
      localWallpaperViewHolder.image01.setTag(Integer.valueOf(localWallpaperBean1.get_id()));
      localWallpaperViewHolder.image01.setContentDescription(String.valueOf(localWallpaperBean1.get_id()));
      localWallpaperViewHolder.image01.setBackgroundDrawable(null);
      new DownloadImageTask(localWallpaperViewHolder.image01, localWallpaperBean1, this.context, "small", null).execute(new String[] { "" });
      localWallpaperViewHolder.image01.setOnClickListener(new View.OnClickListener()
      {
        public void onClick(View paramAnonymousView)
        {
          Intent localIntent = new Intent(WallpaperListAdapter.this.context, CropActivity.class);
          localIntent.putExtra("wallpaperType", "appInner");
          localIntent.putExtra("wallpaperValue", paramAnonymousView.getTag().toString());
          WallpaperListAdapter.this.context.startActivity(localIntent);
        }
      });
      if (1 + paramInt * 2 >= this.listWallpaperBean.size())
        break;
      WallpaperBean localWallpaperBean2 = (WallpaperBean)this.listWallpaperBean.get(1 + paramInt * 2);
      localWallpaperViewHolder.image02.setLayoutParams(new LinearLayout.LayoutParams(this.width, this.height));
      localWallpaperViewHolder.image02.setTag(Integer.valueOf(localWallpaperBean2.get_id()));
      localWallpaperViewHolder.image02.setContentDescription(String.valueOf(localWallpaperBean2.get_id()));
      localWallpaperViewHolder.image02.setBackgroundDrawable(null);
      new DownloadImageTask(localWallpaperViewHolder.image02, localWallpaperBean2, this.context, "small", null).execute(new String[] { "" });
      localWallpaperViewHolder.image02.setOnClickListener(new View.OnClickListener()
      {
        public void onClick(View paramAnonymousView)
        {
          Intent localIntent = new Intent(WallpaperListAdapter.this.context, CropActivity.class);
          localIntent.putExtra("wallpaperType", "appInner");
          localIntent.putExtra("wallpaperValue", paramAnonymousView.getTag().toString());
          WallpaperListAdapter.this.context.startActivity(localIntent);
        }
      });
      return paramView;
      localWallpaperViewHolder = (WallpaperViewHolder)paramView.getTag();
    }
    localWallpaperViewHolder.image02.setLayoutParams(new LinearLayout.LayoutParams(this.width, this.height));
    localWallpaperViewHolder.image02.setBackgroundDrawable(null);
    localWallpaperViewHolder.image02.setOnClickListener(null);
    return paramView;
  }

  public void removeItemById(int paramInt)
  {
    Iterator localIterator;
    if (this.listWallpaperBean != null)
      localIterator = this.listWallpaperBean.iterator();
    WallpaperBean localWallpaperBean;
    do
    {
      if (!localIterator.hasNext())
        return;
      localWallpaperBean = (WallpaperBean)localIterator.next();
    }
    while ((localWallpaperBean == null) || (localWallpaperBean.get_id() != paramInt));
    this.listWallpaperBean.remove(localWallpaperBean);
  }

  public class DownloadImageTask extends AsyncTask<String, Integer, String>
  {
    private WallpaperListAdapter.DownloadImageTaskAfter after;
    private Context context;
    private ImageView iv;
    private String type;
    private WallpaperBean wb;

    public DownloadImageTask(ImageView paramWallpaperBean, WallpaperBean paramContext, Context paramString, String paramDownloadImageTaskAfter, WallpaperListAdapter.DownloadImageTaskAfter arg6)
    {
      this.iv = paramWallpaperBean;
      this.wb = paramContext;
      this.context = paramString;
      this.type = paramDownloadImageTaskAfter;
      Object localObject;
      this.after = localObject;
    }

    private String download(String paramString1, String paramString2, String paramString3, boolean paramBoolean)
      throws IOException
    {
      FileOutputStream localFileOutputStream = null;
      InputStream localInputStream = null;
      try
      {
        localInputStream = new URL(paramString1).openConnection().getInputStream();
        localFileOutputStream = null;
        byte[] arrayOfByte;
        if (!paramBoolean)
        {
          localFileOutputStream = this.context.openFileOutput(paramString3, 1);
          arrayOfByte = new byte[512];
        }
        while (true)
        {
          int i = localInputStream.read(arrayOfByte);
          if (i == -1)
          {
            localFileOutputStream.flush();
            return "SUCCESS";
            localFileOutputStream = new FileOutputStream(new File(paramString2));
            break;
          }
          localFileOutputStream.write(arrayOfByte, 0, i);
        }
      }
      catch (MalformedURLException localMalformedURLException)
      {
        localMalformedURLException.printStackTrace();
        return "FAIL";
      }
      finally
      {
        if (localFileOutputStream != null)
          localFileOutputStream.close();
        if (localInputStream != null)
          localInputStream.close();
      }
    }

    protected String doInBackground(String[] paramArrayOfString)
    {
      String str1 = this.wb.getLocalSmall();
      if (!"small".equals(this.type))
        str1 = this.wb.getLocalLarge();
      boolean bool1 = "web".equals(this.wb.getType());
      int i = 0;
      String str2;
      label90: int j;
      String str3;
      if (bool1)
      {
        if ((str1 == null) || ("".equals(str1)))
          i = 1;
      }
      else
      {
        if (i == 0)
          break label449;
        if (!"small".equals(this.type))
          break label164;
        str2 = this.wb.getUrlSmall();
        j = 1;
        if ((!Global.isSDCard()) || (Global.getSDCardSize(this.context) < 30))
          break label176;
        str3 = WallpaperListAdapter.this.pathSDCard;
      }
      while (true)
      {
        if (str3 != null)
          break label206;
        return "STORAGE_SIZE";
        File localFile2 = new File(str1);
        if (localFile2 != null)
        {
          boolean bool2 = localFile2.exists();
          i = 0;
          if (bool2)
            break;
        }
        i = 1;
        break;
        label164: str2 = this.wb.getUrlLarge();
        break label90;
        label176: int k = Global.getMemorySize();
        str3 = null;
        if (k >= 30)
        {
          str3 = WallpaperListAdapter.this.pathMemory;
          j = 0;
        }
      }
      label206: String str4 = str2.substring(1 + str2.lastIndexOf("/"));
      String str5 = str3 + str4;
      if (j == 0);
      try
      {
        String str6 = download(str2, str5, str4, false);
        str5 = new StringBuilder(String.valueOf(this.context.getFilesDir().getParent())).append(File.separator).append(this.context.getFilesDir().getName()).toString() + File.separator + str4;
        if ("SUCCESS".equals(str6))
        {
          if ("small".equals(this.type))
            this.wb.setLocalSmall(str5);
          while (true)
          {
            new DBHelper(this.context).updateWallpaper(this.wb);
            return "SUCCESS";
            File localFile1 = new File(str3);
            if (!localFile1.exists())
              localFile1.mkdirs();
            str6 = download(str2, str5, str4, true);
            break;
            this.wb.setLocalLarge(str5);
          }
        }
      }
      catch (IOException localIOException)
      {
        localIOException.printStackTrace();
        return "FAIL";
      }
      return "FAIL";
      label449: return "SUCCESS";
    }

    public boolean isNetworkAvailable()
    {
      NetworkInfo localNetworkInfo = ((ConnectivityManager)this.context.getSystemService("connectivity")).getActiveNetworkInfo();
      if (localNetworkInfo == null)
        return false;
      return localNetworkInfo.isConnected();
    }

    protected void onPostExecute(String paramString)
    {
      boolean bool = true;
      if ("SUCCESS".equals(paramString))
        if ("appInner".equals(this.wb.getType()))
          this.iv.setBackgroundResource(this.context.getResources().getIdentifier(this.wb.getName(), "drawable", this.context.getPackageName()));
      while (true)
      {
        if (this.after != null)
          this.after.doOther(paramString);
        super.onPostExecute(paramString);
        return;
        if (("web".equals(this.wb.getType())) && ("small".equals(this.type)))
        {
          File localFile = new File(this.wb.getLocalSmall());
          if ((localFile != null) && (localFile.exists()))
          {
            this.iv.setBackgroundDrawable(new BitmapDrawable(ImageHelper.getBitmapFromFile(this.wb.getLocalSmall(), this.iv.getWidth(), this.iv.getHeight())));
            continue;
            if ("FAIL".equals(paramString))
            {
              if (!WallpaperListAdapter.this.tipNotNet)
              {
                if (isNetworkAvailable())
                  break label279;
                WallpaperListAdapter localWallpaperListAdapter3 = WallpaperListAdapter.this;
                if (WallpaperListAdapter.this.tipNotNet)
                  bool = false;
                localWallpaperListAdapter3.tipNotNet = bool;
                Toast.makeText(this.context, 2131428051, 0).show();
              }
              while (true)
              {
                if (!"small".equals(this.type))
                  break label328;
                WallpaperListAdapter.this.removeItemById(this.wb.get_id());
                WallpaperListAdapter.this.notifyDataSetChanged();
                break;
                label279: if (!WallpaperListAdapter.this.tipWallpaperDownloadFail)
                {
                  WallpaperListAdapter localWallpaperListAdapter2 = WallpaperListAdapter.this;
                  if (WallpaperListAdapter.this.tipWallpaperDownloadFail)
                    bool = false;
                  localWallpaperListAdapter2.tipWallpaperDownloadFail = bool;
                  Toast.makeText(this.context, 2131427339, 0).show();
                }
              }
            }
            else
            {
              label328: if (("STORAGE_SIZE".equals(paramString)) && (!WallpaperListAdapter.this.tipStorageSize))
              {
                WallpaperListAdapter localWallpaperListAdapter1 = WallpaperListAdapter.this;
                if (WallpaperListAdapter.this.tipStorageSize)
                  bool = false;
                localWallpaperListAdapter1.tipStorageSize = bool;
                Toast.makeText(this.context, 2131427437, 0).show();
              }
            }
          }
        }
      }
    }
  }

  public static abstract interface DownloadImageTaskAfter
  {
    public abstract void doOther(String paramString);
  }

  public class WallpaperViewHolder
  {
    public ImageView image01;
    public ImageView image02;

    public WallpaperViewHolder()
    {
    }
  }
}

/* Location:           C:\Documents and Settings\Administrator\classes_dex2jar.jar
 * Qualified Name:     com.appwoo.txtws.adapter.WallpaperListAdapter
 * JD-Core Version:    0.6.2
 */